package eu.news.kits;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eu.jk.config.Config;
import eu.jk.config.ConfigManager;
import eu.jk.helper.RootPath;
import eu.news.config.Debugger;
import eu.news.data.pojo.Theme;

public class Backman {
	
	private static Map<String, Theme> themeMap=new HashMap<String, Theme>();
	
	public static List<Theme> ListThemes(){
		List<Theme> themes=new ArrayList<Theme>();
		File dir=new File(RootPath.root+"/../../template/");
		for(File file:dir.listFiles()){
			if(file.isFile())
				continue;
			try {
				Config info=ConfigManager.loadConfigFile(file.getAbsolutePath()+"/theme.info").getConfig("info");
				Theme theme=new Theme(info.getValueNE("name"), file.getName());
				theme.setAuthor(info.getValueNE("author"));
				theme.setVersion(info.getValueNE("version"));
				theme.setDescribe(info.getValueNE("describe"));
				theme.setCompatible(info.getValueNE("compatible"));
				themeMap.put(theme.getPath(), theme);
				//TODO 我觉得这里可以优化，你觉得呢？
				themes.add(theme);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return themes;
	}
	
	public static Theme getThemeByPath(String path){
		Theme theme=themeMap.get(path);
		if(theme==null){
			try {
				Config info=ConfigManager.loadConfigFile(RootPath.root+"../../template/"+path+"/theme.info").getConfig("info");
				theme=new Theme(info.getValueNE("name"), path);
				theme.setAuthor(info.getValueNE("author"));
				theme.setVersion(info.getValueNE("version"));
				theme.setDescribe(info.getValueNE("describe"));
				theme.setCompatible(info.getValueNE("compatible"));
				themeMap.put(theme.getPath(), theme);
			} catch (FileNotFoundException e) {
				Debugger.printStackTrace(e);
				return null;
			}
		}
		return theme;
	}
}
